package com.fenghui.doubantop.utils;

import com.fenghui.doubantop.domain.Music;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * 音乐解析工具类，继承自抽象的解析工具类
 */
public class MusicParseUtil extends ParseUtil<Music> {

    @Override
    protected void extractInfo(Element child, Music music) {
        // 专辑代码
        String musicId = child.select("table tbody tr").attr("id").trim();
        music.setMusicId(Integer.valueOf(musicId));

        // 专辑封面图片 URL
        String musicUrl = child.select("table tbody tr td a img").attr("src").trim();
        music.setMusicUrl(musicUrl);

        // 专辑名称
        String musicTitle = child.select("table tbody tr td div a").text().trim();
        music.setMusicTitle(musicTitle);

        //查询音乐信息块，并从中取表演者，发现时间等
        String p1 = child.select("table tbody tr td div p").text().trim();
        String[] ss = p1.split(" / ");

        // 表演者
        String performer = ss[0];
        music.setPerformer(performer);

        // 发行时间
        String releaseTime = ss[1];
        music.setReleaseTime(releaseTime);

        String musicType = "", medium = "", sect = "";
        if (ss.length == 4) {
            //介质
            medium = ss[2];

            // 流派
            sect = ss[3];

        } else {
            //专辑类型
            musicType = ss[2];
            //介质
            medium = ss[3];
            // 流派
            sect = ss[4];
        }
        music.setMusicType(musicType);
        music.setMedium(medium);
        music.setSect(sect);

        // 评分
        String ratingNum = child.select("table tbody tr td div div span.rating_nums").text().trim();
        music.setRatingNum(Double.valueOf(ratingNum));

        // 评价人次
        String remarkNum = child.select("table tbody tr td div div span.pl").text().trim();
        music.setRemarkNum(Integer.valueOf(BaseUtils.extractNumber(remarkNum)));
    }


    @Override
    protected Element getElement(Document document) {
        return document.getElementsByClass("indent").get(0);
    }

    @Override
    protected boolean isIgnoredTag(Element element) {
        return "p".equals(element.tagName()) || "div".equals(element.tagName());
    }

    @Override
    protected Music createItem() {
        return new Music();
    }
}
